package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.ActivityFeedback;
import com.tju.ocean_ecology_website.dao.ActivityFeedbackDao;
import com.tju.ocean_ecology_website.service.ActivityFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 活动反馈表(ActivityFeedback)表服务实现类
 *
 * @author shaolong
 * @since 2025-05-05 10:00:00
 */
@Service("activityFeedbackService")
public class ActivityFeedbackServiceImpl implements ActivityFeedbackService {
    @Resource
    private ActivityFeedbackDao activityFeedbackDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ActivityFeedback queryById(Long id) {
        return this.activityFeedbackDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param activityFeedback 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ActivityFeedback> queryByPage(ActivityFeedback activityFeedback, PageRequest pageRequest) {
        long total = this.activityFeedbackDao.count(activityFeedback);
        return new PageImpl<>(this.activityFeedbackDao.queryAllByLimit(activityFeedback, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param activityFeedback 实例对象
     * @return 实例对象
     */
    @Override
    public ActivityFeedback insert(ActivityFeedback activityFeedback) {
        this.activityFeedbackDao.insert(activityFeedback);
        return activityFeedback;
    }

    /**
     * 修改数据
     *
     * @param activityFeedback 实例对象
     * @return 实例对象
     */
    @Override
    public ActivityFeedback update(ActivityFeedback activityFeedback) {
        this.activityFeedbackDao.update(activityFeedback);
        return this.queryById(activityFeedback.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.activityFeedbackDao.deleteById(id) > 0;
    }

    /**
     * 根据活动ID查询反馈
     *
     * @param activityId 活动ID
     * @param pageRequest 分页对象
     * @return 反馈列表
     */
    @Override
    public Page<ActivityFeedback> queryByActivityId(Long activityId, PageRequest pageRequest) {
        long total = this.activityFeedbackDao.countByActivityId(activityId);
        List<ActivityFeedback> feedbacks = this.activityFeedbackDao.queryByActivityId(activityId, pageRequest);
        return new PageImpl<>(feedbacks, pageRequest, total);
    }

    /**
     * 根据用户ID和活动ID查询反馈
     *
     * @param userId 用户ID
     * @param activityId 活动ID
     * @return 反馈对象
     */
    @Override
    public ActivityFeedback queryByUserAndActivity(Long userId, Long activityId) {
        return this.activityFeedbackDao.queryByUserAndActivity(userId, activityId);
    }
}
